package com.hgyc.mom.system.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.system.service.SysUserService;
import com.hgyc.mom.system.vo.LoginVO;
import com.hgyc.mom.system.vo.UserInfoVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录接口
 * @author fwj
 */
@RestController
@RequestMapping("/")
@Tag(name = "登录接口", description = "登录接口")
public class SysLoginController {

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result<UserInfoVO> doLogin(@RequestBody LoginVO loginVO) {
        sysUserService.login(loginVO);
        UserInfoVO userInfo = sysUserService.getUserInfo();

        return Result.success("登录成功", userInfo);
    }

    @RequestMapping("isLogin")
    public SaResult isLogin() {
        return SaResult.ok("是否登录：" + StpUtil.isLogin());
    }

    @RequestMapping("tokenInfo")
    public SaResult tokenInfo() {
        return SaResult.data(StpUtil.getTokenInfo());
    }

    @RequestMapping("/logout")
    public SaResult logout() {
        StpUtil.logout();
        return SaResult.ok();
    }

}
